﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Himp.Erp
{
    /// <summary>
    /// 商品入库
    /// </summary>
    public class InvManager : DomainService
    {
        private IRepository<InboundRec, Guid> _inboundRecRepository;
        private IRepository<OutboundRec, Guid> _outboundRecRepository;
        private IRepository<MatlInv, Guid> _matlInvRepository;

        public InvManager(IRepository<InboundRec, Guid> inboundRecRepository
            , IRepository<OutboundRec, Guid> outboundRecRepository
            , IRepository<MatlInv, Guid> matlInvRepository)
        {
            _inboundRecRepository = inboundRecRepository;
            _outboundRecRepository = outboundRecRepository;
            _matlInvRepository = matlInvRepository;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async  Task<List<MatlInv>> ChkInboundRec(Guid id)
        {
            List<MatlInv> matlInvs = new List<MatlInv>(); 
            var inboundRec = await _inboundRecRepository.GetAsync(id);

            if (inboundRec.InboundStas == EnumInboundStas.未审核)
            {
                inboundRec.InboundStas = EnumInboundStas.已审核;

                foreach (var inboundDetl in inboundRec.InboundDetls)
                {
                    MatlInv matlInv = new MatlInv();
                    matlInv.MatlWhseId = inboundRec.MatlWhseId;
                    matlInv.MatlInfoId = inboundDetl.MatlInfoId;
                    matlInv.InboundDetlId = inboundDetl.Id;
                    matlInv.Bchno = inboundDetl.Bchno;
                    matlInv.InvChgTypeCode = inboundRec.InvChgTypeCode;
                    matlInv.InvChgTypeName = inboundRec.InvChgTypeName;
                    matlInv.BizType = EnumBizType.入库;
                    matlInv.ManuLotnum = inboundRec.OrdNo;
                    matlInv.ValiDate = inboundDetl.ValiDatetime;
                    matlInv.CurFlag = true;
                    matlInv.Ver = 0;
                    matlInv.TotalCnt = 0;
                    matlInv.ChgCnt = inboundDetl.Cnt;
                    matlInv.FinlCnt = inboundDetl.Cnt;

                    matlInvs.Add(matlInv);
                }
            }

            return matlInvs;
        }

        public async Task<List<MatlInv>> ChkOutboundRec(Guid id)
        {
            List<MatlInv> matlInvs = new List<MatlInv>();
            var outboundRec = await _outboundRecRepository.GetAsync(id);

            if (outboundRec.OutboundStas == EnumOutboundStas.未审核)
            {
                outboundRec.OutboundStas = EnumOutboundStas.已审核;

                foreach (var outboundDetl in outboundRec.OutboundDetls)
                {
                    var curMatlInv = await _matlInvRepository.GetAsync(m => m.InboundDetlId == outboundDetl.InboundDetlId);
                    curMatlInv.CurFlag = false;

                    MatlInv matlInv = new MatlInv();
                    matlInv.MatlWhseId = outboundRec.MatlWhseId;
                    matlInv.MatlInfoId = outboundDetl.MatlInfoId;
                    matlInv.InboundDetlId = outboundDetl.InboundDetlId;
                    matlInv.Bchno = outboundDetl.Bchno;
                    matlInv.InvChgTypeCode = outboundRec.InvChgTypeCode;
                    matlInv.InvChgTypeName = outboundRec.InvChgTypeName;
                    matlInv.BizType = EnumBizType.出库;
                    matlInv.ManuLotnum = outboundRec.OrdNo;
                    matlInv.ValiDate = DateTime.Now;
                    matlInv.CurFlag = true;
                    matlInv.Ver = curMatlInv.Ver++;
                    matlInv.TotalCnt = curMatlInv.FinlCnt;
                    matlInv.ChgCnt = outboundDetl.Cnt;
                    matlInv.FinlCnt = curMatlInv.FinlCnt - outboundDetl.Cnt;

                    matlInvs.Add(matlInv);
                }
            }

            return matlInvs;
        }

        ///// <summary>
        ///// 创建入库单
        ///// </summary>
        ///// <returns></returns>
        //public async Task<InboundRec> CreateAsync(InboundRec purchaseInWarehouse)
        //{
        //    var newPurchaseInWarehouse = await _purchaseInWarehouseRepository.InsertAsync(purchaseInWarehouse, true);
        //    return newPurchaseInWarehouse;
        //}

        ///// <summary>
        ///// 修改入库单
        ///// </summary>
        ///// <returns></returns>
        //public async Task<InboundRec> EditAsync(InboundRec purchaseInWarehouse)
        //{
        //    var newPurchaseInWarehouse = await _purchaseInWarehouseRepository.UpdateAsync(purchaseInWarehouse, true);

        //    return newPurchaseInWarehouse;
        //}
    }
}
